using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace rocketjump
{
    public static class GraphicsHelper
    {
        private static Boolean batch_begun = false;
        private static SpriteBatch sBatch = null;
        private static Game parent = null;
        public static void Initialize(Game game, SpriteBatch spriteBatch)
        {
            sBatch = spriteBatch;
            parent = game;
        }

        public static Texture2D LoadTexture(String name)
        {
            return parent.Content.Load<Texture2D>(name);
        }

        public static void DrawSprite(Sprite sprite)
        {
            Draw(sprite.texture, sprite.position, sprite.scale, sprite.rotation); 
        }

        public static void Draw(Texture2D texture, Vector2 position, float scale, float rotation, SpriteEffects effect)
        {
            BeginBatch();
            Vector2 origin = new Vector2();
            origin.X = texture.Width / 2 * scale;
            origin.Y = texture.Height / 2 * scale;
            sBatch.Draw(texture, position, null, Color.White, rotation, origin, scale, effect, 1f);        
        }

        public static void Draw(Texture2D texture, Vector2 position, float scale, float rotation)
        {
            Draw(texture, position, scale, rotation, SpriteEffects.None);
        }

        public static void DrawParticleEmitter(ParticleEngine emitter)
        {
            //dBatch();
            emitter.Draw(sBatch);
        }

        public static void DrawFrameVertical(Texture2D spritesheet, Vector2 position, float scale, float rotation, int frame, int frameCount)
        {
            Vector2 origin = new Vector2();

            int frameHeight = spritesheet.Height / frameCount;
            Rectangle sourceRect = new Rectangle(0, frameHeight * frame, spritesheet.Width, frameHeight);

            origin.X = sourceRect.Width / 2 * scale;
            origin.Y = sourceRect.Height / 2 * scale;

            sBatch.Draw(spritesheet, position, sourceRect, Color.White, rotation, origin, scale, SpriteEffects.None, 0f);
        }

        public static void DrawStringCentered(SpriteFont font, String text, float Y, Color color)
        {
            BeginBatch();
            Vector2 size = font.MeasureString(text);

            Vector2 position = new Vector2(400 - size.X/2, Y);

            if(color != Color.Black)
                sBatch.DrawString(font, text, position - new Vector2(-2,-2), Color.Black);
            sBatch.DrawString(font, text, position, color);
        }

        public static void DrawFrameHorizontal(Texture2D spritesheet, Vector2 position, float scale, float rotation, int frame, int frameCount)
        {
            BeginBatch();
            Vector2 origin = new Vector2();

            int frameWidth = spritesheet.Width / frameCount;
            Rectangle sourceRect = new Rectangle(frameWidth * frame, 0, frameWidth, spritesheet.Height);

            origin.X = sourceRect.Width / 2 * scale;
            origin.Y = sourceRect.Height / 2 * scale;

            sBatch.Draw(spritesheet, position, sourceRect, Color.White, rotation, origin, scale, SpriteEffects.None, 0f);
        }

        public static void BeginBatch()
        {
            if (batch_begun)
                return;
            batch_begun = true;
            sBatch.Begin();
        }

        public static void EndBatch()
        {
            if (batch_begun == false)
                return;
            sBatch.End();
            batch_begun = false;
        }

        public static Vector2 InterpolateVector(Vector2 v, float radians)
        {
            float angle = (float)Math.Atan2(v.Y, v.X);
            angle += radians;
            return new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle)) * v.Length();            
        }

        public static float GetAngle(Vector2 v)
        {
            return (float)Math.Atan2(v.Y, v.X);
        }
    }
}